{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4ed21505-0445-4d7c-b41a-cf48aaf358be",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import os\n",
    "thepath1=os.getcwd()+r\"\\northernireland1999.csv\"\n",
    "thepath2=os.getcwd()+r\"\\northernireland2008.csv\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "21657912-603d-49f0-bd69-59697b6ca56c",
   "metadata": {},
   "outputs": [],
   "source": [
    "#The 1999 IVS survey for Northern Ireland is available through EVS (2011). EVS - European Values Study 1999 - Integrated Dataset (ZA3811; Version 3.0.0) [Data set]. GESIS, Cologne. https://doi.org/10.4232/1.10789\n",
    "# The 2008 IVS survey for Northern Ireland is available through EVS (2022). European Values Study 2008: Integrated Dataset (EVS 2008) (ZA4800; Version 5.0.0) [Data set]. GESIS, Cologne. https://doi.org/10.4232/1.13841\n",
    "ni99=pd.read_csv(thepath1)\n",
    "ni08=pd.read_csv(thepath2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "95bdf9c3-774f-449b-81da-c1d9e80e54c2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>c_abrv</th>\n",
       "      <th>v98</th>\n",
       "      <th>v102</th>\n",
       "      <th>v104</th>\n",
       "      <th>v256</th>\n",
       "      <th>weight_g</th>\n",
       "      <th>Year</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>40125</td>\n",
       "      <td>GB-NIR</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>90903.0</td>\n",
       "      <td>1.805054</td>\n",
       "      <td>1999</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>40126</td>\n",
       "      <td>GB-NIR</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.902527</td>\n",
       "      <td>1999</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>40127</td>\n",
       "      <td>GB-NIR</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>90902.0</td>\n",
       "      <td>0.902527</td>\n",
       "      <td>1999</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>40128</td>\n",
       "      <td>GB-NIR</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.902527</td>\n",
       "      <td>1999</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>40129</td>\n",
       "      <td>GB-NIR</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>90903.0</td>\n",
       "      <td>0.902527</td>\n",
       "      <td>1999</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Unnamed: 0  c_abrv  v98  v102  v104     v256  weight_g  Year\n",
       "0       40125  GB-NIR  1.0   3.0   NaN  90903.0  1.805054  1999\n",
       "1       40126  GB-NIR  1.0   3.0   NaN      NaN  0.902527  1999\n",
       "2       40127  GB-NIR  1.0   1.0   NaN  90902.0  0.902527  1999\n",
       "3       40128  GB-NIR  1.0   NaN   NaN      NaN  0.902527  1999\n",
       "4       40129  GB-NIR  1.0   2.0   NaN  90903.0  0.902527  1999"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ni99.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a5ce3845-9412-4bc1-9dce-9e253128ec67",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>c_abrv</th>\n",
       "      <th>v102</th>\n",
       "      <th>v106</th>\n",
       "      <th>v108</th>\n",
       "      <th>v264</th>\n",
       "      <th>weight_g</th>\n",
       "      <th>Year</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>64179</td>\n",
       "      <td>GB-NIR</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>-3</td>\n",
       "      <td>90905</td>\n",
       "      <td>0.856497</td>\n",
       "      <td>2008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>64180</td>\n",
       "      <td>GB-NIR</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>-3</td>\n",
       "      <td>-3</td>\n",
       "      <td>1.607589</td>\n",
       "      <td>2008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>64181</td>\n",
       "      <td>GB-NIR</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>-3</td>\n",
       "      <td>90901</td>\n",
       "      <td>0.973039</td>\n",
       "      <td>2008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>64182</td>\n",
       "      <td>GB-NIR</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>-3</td>\n",
       "      <td>-3</td>\n",
       "      <td>0.856497</td>\n",
       "      <td>2008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>64183</td>\n",
       "      <td>GB-NIR</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>-3</td>\n",
       "      <td>90901</td>\n",
       "      <td>0.841883</td>\n",
       "      <td>2008</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Unnamed: 0  c_abrv  v102  v106  v108   v264  weight_g  Year\n",
       "0       64179  GB-NIR     1     2    -3  90905  0.856497  2008\n",
       "1       64180  GB-NIR     1     2    -3     -3  1.607589  2008\n",
       "2       64181  GB-NIR     1     2    -3  90901  0.973039  2008\n",
       "3       64182  GB-NIR     2     2    -3     -3  0.856497  2008\n",
       "4       64183  GB-NIR     1     2    -3  90901  0.841883  2008"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ni08.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e148bf93-47f6-483a-a63c-8ac5ea837a4a",
   "metadata": {},
   "outputs": [],
   "source": [
    "def codeevsagree(df,avar):\n",
    "    conditions = [\n",
    "      (df[avar]==1)\n",
    "    , (df[avar]==2)\n",
    "    , (df[avar]==3)\n",
    "]\n",
    "    return(conditions)\n",
    "\n",
    "choices3  = [\n",
    "    \"agree\"\n",
    "    , \"disagree\"\n",
    "    , \"neutral\"\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "223d5934-c22a-456f-a03a-8d20e73e0e11",
   "metadata": {},
   "outputs": [],
   "source": [
    "ni99[\"job_rgnl\"] = np.select(codeevsagree(ni99,'v98'), choices3, default=None)\n",
    "ni08['job_rgnl'] = np.select(codeevsagree(ni08,'v102'), choices3, default=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "41f17ca9-b078-4335-9bb5-e3965d4a49b5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "relig\n",
       "Protestant        204\n",
       "Roman Catholic    170\n",
       "Other              25\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Do you belong to a religious denomination? Which one?\n",
    "# -5 other missing\n",
    "# -4 question not asked\n",
    "# -3 not applicable\n",
    "# -2 no answer\n",
    "# -1 don't know\n",
    "# 1 Roman Catholic\n",
    "# 2 Protestant\n",
    "# 3 Free Church/ Non-Conformist/ Evangelical\n",
    "# 4 Jew\n",
    "# 5 Muslim\n",
    "# 6 Hindu\n",
    "# 7 Buddhist\n",
    "# 8 Orthodox\n",
    "# 9 other\n",
    "religdict={1:'Roman Catholic',\n",
    "2:'Protestant',\n",
    "3:'Protestant',\n",
    "4:'Other',\n",
    "5:'Other',\n",
    "6:'Other',\n",
    "7:'Other',\n",
    "8:'Other',\n",
    "9:'Other'}\n",
    "\n",
    "ni99['relig']=ni99['v102'].replace(religdict)\n",
    "ni99.loc[pd.isna(ni99['relig']),'relig']=ni99['v104'].replace(religdict) #Previous religion\n",
    "#2008 version\n",
    "ni08['relig']=ni08['v106'].replace(religdict)\n",
    "ni08.loc[pd.isna(ni08['relig']),'relig']=ni08['v108'].replace(religdict) #Previous religion\n",
    "ni08.loc[ni08['v106']<0,'relig']=np.nan\n",
    "ni08.relig.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "40d613d2-ca2a-4566-994e-063147a9200e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "partygrps\n",
       "Moderate Unionist             244\n",
       "Moderate Irish nationalist    199\n",
       "Hardline Unionist             120\n",
       "None/Other                    109\n",
       "Non-sectarian                  76\n",
       "Hardline Irish nationalist     67\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#If there was a general election tomorrow, which party would you vote for?\n",
    "ni99parties99groups={\n",
    "96:'None/Other',\n",
    "97:'None/Other',\n",
    "90901:'Moderate Unionist', #Ulster Unionist'\n",
    "90903:'Hardline Unionist', #DUP\n",
    "90904:'Non-sectarian', #Alliance\n",
    "90905:'Hardline Irish nationalist', #Sinn Fein\n",
    "90902:'Moderate Irish nationalist', #SDLP\n",
    "90906:'Moderate Unionist',#Progressive Unionist' Party (PUP) \n",
    "90907:'Hardline Unionist',#The Ulster Democratic Party (UDP)\n",
    "90908:'Hardline Unionist', #The UK Unionist' Party (UKUP) \n",
    "90909:'Non-sectarian', #Women's Coalition\n",
    "90910:'Hardline Irish nationalist', #Workers party\n",
    "90911:'Non-sectarian', #Green party\n",
    "90912:'None/Other'\n",
    "}\n",
    "ni99['partygrps']=ni99['v256'].replace(ni99parties99groups)\n",
    "ni99.partygrps.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "bcf467ce-47af-4fb2-9cf4-7e86aed8184c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>partygrps</th>\n",
       "      <th>Hardline Irish nationalist</th>\n",
       "      <th>Hardline Unionist</th>\n",
       "      <th>Moderate Irish nationalist</th>\n",
       "      <th>Moderate Unionist</th>\n",
       "      <th>Non-sectarian</th>\n",
       "      <th>None/Other</th>\n",
       "      <th>All</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>relig</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Other</th>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>8</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Protestant</th>\n",
       "      <td>2</td>\n",
       "      <td>105</td>\n",
       "      <td>8</td>\n",
       "      <td>220</td>\n",
       "      <td>48</td>\n",
       "      <td>49</td>\n",
       "      <td>432</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Roman Catholic</th>\n",
       "      <td>64</td>\n",
       "      <td>0</td>\n",
       "      <td>186</td>\n",
       "      <td>1</td>\n",
       "      <td>22</td>\n",
       "      <td>42</td>\n",
       "      <td>315</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>All</th>\n",
       "      <td>66</td>\n",
       "      <td>110</td>\n",
       "      <td>195</td>\n",
       "      <td>229</td>\n",
       "      <td>73</td>\n",
       "      <td>94</td>\n",
       "      <td>767</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "partygrps       Hardline Irish nationalist  Hardline Unionist  \\\n",
       "relig                                                           \n",
       "Other                                    0                  5   \n",
       "Protestant                               2                105   \n",
       "Roman Catholic                          64                  0   \n",
       "All                                     66                110   \n",
       "\n",
       "partygrps       Moderate Irish nationalist  Moderate Unionist  Non-sectarian  \\\n",
       "relig                                                                          \n",
       "Other                                    1                  8              3   \n",
       "Protestant                               8                220             48   \n",
       "Roman Catholic                         186                  1             22   \n",
       "All                                    195                229             73   \n",
       "\n",
       "partygrps       None/Other  All  \n",
       "relig                            \n",
       "Other                    3   20  \n",
       "Protestant              49  432  \n",
       "Roman Catholic          42  315  \n",
       "All                     94  767  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.crosstab(ni99.relig,ni99.partygrps,margins=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "1b1394f2-c094-4ef3-b153-61f79351dd0f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "partygrps\n",
       "Hardline Unionist             87\n",
       "Moderate Irish nationalist    54\n",
       "Hardline Irish nationalist    44\n",
       "Moderate Unionist             40\n",
       "Non-sectarian                 15\n",
       "None/Other                     6\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "niparties08groups={\n",
    "90901:'Hardline Unionist', #DUP\n",
    "90902:'Hardline Irish nationalist', #Sinn Fein\n",
    "90903:'Moderate Irish nationalist', #SDLP\n",
    "90904:'Non-sectarian', #Alliance\n",
    "90905:'Moderate Unionist', #UUP\n",
    "90906:'Moderate Unionist', #PUP\n",
    "90907:'Hardline Unionist', #UK Unionist Party\n",
    "90908:'Hardline Unionist', #Conservative\n",
    "90909:'Hardline Irish nationalist', #Workers party\n",
    "90910:'Moderate Irish nationalist', #Socialist environmentalist alliance\n",
    "90911:'Non-sectarian', #Socialist party\n",
    "90912:'Non-sectarian', #Green party\n",
    "90996:'None/Other',\n",
    "96:'None/Other'\n",
    "}\n",
    "ni08.loc[(ni08['v264']<0),'v264']=np.nan\n",
    "ni08['partygrps']=ni08['v264'].replace(niparties08groups)\n",
    "ni08.partygrps.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "49499587-d535-430b-8a9e-60bad19be6b5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>partygrps</th>\n",
       "      <th>Hardline Irish nationalist</th>\n",
       "      <th>Hardline Unionist</th>\n",
       "      <th>Moderate Irish nationalist</th>\n",
       "      <th>Moderate Unionist</th>\n",
       "      <th>Non-sectarian</th>\n",
       "      <th>None/Other</th>\n",
       "      <th>All</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>relig</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Other</th>\n",
       "      <td>1</td>\n",
       "      <td>8</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Protestant</th>\n",
       "      <td>1</td>\n",
       "      <td>56</td>\n",
       "      <td>1</td>\n",
       "      <td>33</td>\n",
       "      <td>7</td>\n",
       "      <td>1</td>\n",
       "      <td>99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Roman Catholic</th>\n",
       "      <td>41</td>\n",
       "      <td>3</td>\n",
       "      <td>49</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "      <td>3</td>\n",
       "      <td>104</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>All</th>\n",
       "      <td>43</td>\n",
       "      <td>67</td>\n",
       "      <td>50</td>\n",
       "      <td>35</td>\n",
       "      <td>14</td>\n",
       "      <td>4</td>\n",
       "      <td>213</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "partygrps       Hardline Irish nationalist  Hardline Unionist  \\\n",
       "relig                                                           \n",
       "Other                                    1                  8   \n",
       "Protestant                               1                 56   \n",
       "Roman Catholic                          41                  3   \n",
       "All                                     43                 67   \n",
       "\n",
       "partygrps       Moderate Irish nationalist  Moderate Unionist  Non-sectarian  \\\n",
       "relig                                                                          \n",
       "Other                                    0                  1              0   \n",
       "Protestant                               1                 33              7   \n",
       "Roman Catholic                          49                  1              7   \n",
       "All                                     50                 35             14   \n",
       "\n",
       "partygrps       None/Other  All  \n",
       "relig                            \n",
       "Other                    0   10  \n",
       "Protestant               1   99  \n",
       "Roman Catholic           3  104  \n",
       "All                      4  213  "
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.crosstab(ni08.loc[ni08.Year==2008,'relig'],ni08.loc[ni08.Year==2008,'partygrps'],margins=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "4e678438-f5ed-4a07-bb38-0c3b852c241d",
   "metadata": {},
   "outputs": [],
   "source": [
    "def evsfunction(avar,df,asecondvar=None):\n",
    "    use=df.copy()\n",
    "    if asecondvar is None:\n",
    "        use['junk']=1\n",
    "        asecondvar='junk'\n",
    "    use=use.loc[(pd.notna(use[avar]))&(pd.notna(use[asecondvar]))].copy()\n",
    "    use['newsum']=use.groupby([asecondvar])['weight_g'].transform('sum')\n",
    "    use['percent_in_category']=use['weight_g']/use['newsum']*100\n",
    "    toreturn=use.groupby([avar,asecondvar], as_index=False)['percent_in_category'].agg(['sum','count']).reset_index()\n",
    "    toreturn=toreturn.copy().pivot(index=[asecondvar], columns=avar, values=['sum','count']).reset_index()\n",
    "    return(toreturn)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40a41c91-3f6d-4006-a734-0b14d68ad66e",
   "metadata": {},
   "source": [
    "#### Calculate support for Northern Ireland job reservations to make Figure 3.3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "7daefc93-0485-4e7a-8243-bd9ea24ad6bf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>junk</th>\n",
       "      <th colspan=\"3\" halign=\"left\">sum</th>\n",
       "      <th colspan=\"3\" halign=\"left\">count</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>job_rgnl</th>\n",
       "      <th></th>\n",
       "      <th>agree</th>\n",
       "      <th>disagree</th>\n",
       "      <th>neutral</th>\n",
       "      <th>agree</th>\n",
       "      <th>disagree</th>\n",
       "      <th>neutral</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>74.74275</td>\n",
       "      <td>19.176801</td>\n",
       "      <td>6.080449</td>\n",
       "      <td>712.0</td>\n",
       "      <td>189.0</td>\n",
       "      <td>56.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         junk       sum                       count                 \n",
       "job_rgnl          agree   disagree   neutral  agree disagree neutral\n",
       "0           1  74.74275  19.176801  6.080449  712.0    189.0    56.0"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "evsfunction('job_rgnl',ni99)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "88804f32-c0d8-4e49-8556-af54ddd2bc8b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>relig</th>\n",
       "      <th colspan=\"3\" halign=\"left\">sum</th>\n",
       "      <th colspan=\"3\" halign=\"left\">count</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>job_rgnl</th>\n",
       "      <th></th>\n",
       "      <th>agree</th>\n",
       "      <th>disagree</th>\n",
       "      <th>neutral</th>\n",
       "      <th>agree</th>\n",
       "      <th>disagree</th>\n",
       "      <th>neutral</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Other</td>\n",
       "      <td>76.666667</td>\n",
       "      <td>13.333333</td>\n",
       "      <td>10.000000</td>\n",
       "      <td>19.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Protestant</td>\n",
       "      <td>72.095333</td>\n",
       "      <td>19.860973</td>\n",
       "      <td>8.043694</td>\n",
       "      <td>349.0</td>\n",
       "      <td>95.0</td>\n",
       "      <td>35.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Roman Catholic</td>\n",
       "      <td>76.839827</td>\n",
       "      <td>19.588745</td>\n",
       "      <td>3.571429</td>\n",
       "      <td>284.0</td>\n",
       "      <td>81.0</td>\n",
       "      <td>13.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   relig        sum                        count           \\\n",
       "job_rgnl                      agree   disagree    neutral  agree disagree   \n",
       "0                  Other  76.666667  13.333333  10.000000   19.0      4.0   \n",
       "1             Protestant  72.095333  19.860973   8.043694  349.0     95.0   \n",
       "2         Roman Catholic  76.839827  19.588745   3.571429  284.0     81.0   \n",
       "\n",
       "                  \n",
       "job_rgnl neutral  \n",
       "0            2.0  \n",
       "1           35.0  \n",
       "2           13.0  "
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "evsfunction('job_rgnl',ni99,'relig')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "029263ff-6f7b-40c8-a152-0050c38d8beb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>partygrps</th>\n",
       "      <th colspan=\"3\" halign=\"left\">sum</th>\n",
       "      <th colspan=\"3\" halign=\"left\">count</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>job_rgnl</th>\n",
       "      <th></th>\n",
       "      <th>agree</th>\n",
       "      <th>disagree</th>\n",
       "      <th>neutral</th>\n",
       "      <th>agree</th>\n",
       "      <th>disagree</th>\n",
       "      <th>neutral</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Hardline Irish nationalist</td>\n",
       "      <td>77.142857</td>\n",
       "      <td>18.857143</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>49.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Hardline Unionist</td>\n",
       "      <td>71.040724</td>\n",
       "      <td>16.289593</td>\n",
       "      <td>12.669683</td>\n",
       "      <td>78.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Moderate Irish nationalist</td>\n",
       "      <td>73.549884</td>\n",
       "      <td>21.345708</td>\n",
       "      <td>5.104408</td>\n",
       "      <td>141.0</td>\n",
       "      <td>44.0</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Moderate Unionist</td>\n",
       "      <td>74.442191</td>\n",
       "      <td>19.066937</td>\n",
       "      <td>6.490872</td>\n",
       "      <td>179.0</td>\n",
       "      <td>42.0</td>\n",
       "      <td>15.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Non-sectarian</td>\n",
       "      <td>64.516129</td>\n",
       "      <td>34.838710</td>\n",
       "      <td>0.645161</td>\n",
       "      <td>45.0</td>\n",
       "      <td>26.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>None/Other</td>\n",
       "      <td>66.949153</td>\n",
       "      <td>22.881356</td>\n",
       "      <td>10.169492</td>\n",
       "      <td>73.0</td>\n",
       "      <td>24.0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                           partygrps        sum                        count  \\\n",
       "job_rgnl                                  agree   disagree    neutral  agree   \n",
       "0         Hardline Irish nationalist  77.142857  18.857143   4.000000   49.0   \n",
       "1                  Hardline Unionist  71.040724  16.289593  12.669683   78.0   \n",
       "2         Moderate Irish nationalist  73.549884  21.345708   5.104408  141.0   \n",
       "3                  Moderate Unionist  74.442191  19.066937   6.490872  179.0   \n",
       "4                      Non-sectarian  64.516129  34.838710   0.645161   45.0   \n",
       "5                         None/Other  66.949153  22.881356  10.169492   73.0   \n",
       "\n",
       "                           \n",
       "job_rgnl disagree neutral  \n",
       "0            13.0     2.0  \n",
       "1            17.0    12.0  \n",
       "2            44.0     9.0  \n",
       "3            42.0    15.0  \n",
       "4            26.0     1.0  \n",
       "5            24.0    10.0  "
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "evsfunction('job_rgnl',ni99,'partygrps')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "4146cdef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>junk</th>\n",
       "      <th colspan=\"3\" halign=\"left\">sum</th>\n",
       "      <th colspan=\"3\" halign=\"left\">count</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>job_rgnl</th>\n",
       "      <th></th>\n",
       "      <th>agree</th>\n",
       "      <th>disagree</th>\n",
       "      <th>neutral</th>\n",
       "      <th>agree</th>\n",
       "      <th>disagree</th>\n",
       "      <th>neutral</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>74.864191</td>\n",
       "      <td>20.822477</td>\n",
       "      <td>4.313332</td>\n",
       "      <td>365.0</td>\n",
       "      <td>96.0</td>\n",
       "      <td>22.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         junk        sum                       count                 \n",
       "job_rgnl           agree   disagree   neutral  agree disagree neutral\n",
       "0           1  74.864191  20.822477  4.313332  365.0     96.0    22.0"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "evsfunction('job_rgnl',ni08)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "33a6c6c1-7c0a-4e81-acf9-d5c5d3ae9539",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>relig</th>\n",
       "      <th colspan=\"3\" halign=\"left\">sum</th>\n",
       "      <th colspan=\"3\" halign=\"left\">count</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>job_rgnl</th>\n",
       "      <th></th>\n",
       "      <th>agree</th>\n",
       "      <th>disagree</th>\n",
       "      <th>neutral</th>\n",
       "      <th>agree</th>\n",
       "      <th>disagree</th>\n",
       "      <th>neutral</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Other</td>\n",
       "      <td>66.537692</td>\n",
       "      <td>23.091063</td>\n",
       "      <td>10.371245</td>\n",
       "      <td>18.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Protestant</td>\n",
       "      <td>77.012411</td>\n",
       "      <td>19.156630</td>\n",
       "      <td>3.830959</td>\n",
       "      <td>155.0</td>\n",
       "      <td>35.0</td>\n",
       "      <td>8.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Roman Catholic</td>\n",
       "      <td>75.283543</td>\n",
       "      <td>21.638639</td>\n",
       "      <td>3.077818</td>\n",
       "      <td>122.0</td>\n",
       "      <td>37.0</td>\n",
       "      <td>6.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   relig        sum                        count           \\\n",
       "job_rgnl                      agree   disagree    neutral  agree disagree   \n",
       "0                  Other  66.537692  23.091063  10.371245   18.0      5.0   \n",
       "1             Protestant  77.012411  19.156630   3.830959  155.0     35.0   \n",
       "2         Roman Catholic  75.283543  21.638639   3.077818  122.0     37.0   \n",
       "\n",
       "                  \n",
       "job_rgnl neutral  \n",
       "0            2.0  \n",
       "1            8.0  \n",
       "2            6.0  "
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "evsfunction('job_rgnl',ni08,'relig')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "eef5a087-8e41-4029-9c6e-48e7b1050496",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>partygrps</th>\n",
       "      <th colspan=\"3\" halign=\"left\">sum</th>\n",
       "      <th colspan=\"3\" halign=\"left\">count</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>job_rgnl</th>\n",
       "      <th></th>\n",
       "      <th>agree</th>\n",
       "      <th>disagree</th>\n",
       "      <th>neutral</th>\n",
       "      <th>agree</th>\n",
       "      <th>disagree</th>\n",
       "      <th>neutral</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Hardline Irish nationalist</td>\n",
       "      <td>72.114057</td>\n",
       "      <td>27.885943</td>\n",
       "      <td>NaN</td>\n",
       "      <td>31.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Hardline Unionist</td>\n",
       "      <td>86.139977</td>\n",
       "      <td>12.769089</td>\n",
       "      <td>1.090934</td>\n",
       "      <td>71.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Moderate Irish nationalist</td>\n",
       "      <td>76.872845</td>\n",
       "      <td>16.470596</td>\n",
       "      <td>6.656559</td>\n",
       "      <td>41.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Moderate Unionist</td>\n",
       "      <td>70.780267</td>\n",
       "      <td>25.171690</td>\n",
       "      <td>4.048043</td>\n",
       "      <td>30.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Non-sectarian</td>\n",
       "      <td>29.925636</td>\n",
       "      <td>64.432789</td>\n",
       "      <td>5.641575</td>\n",
       "      <td>5.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>None/Other</td>\n",
       "      <td>100.000000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                           partygrps         sum                      count  \\\n",
       "job_rgnl                                   agree   disagree   neutral agree   \n",
       "0         Hardline Irish nationalist   72.114057  27.885943       NaN  31.0   \n",
       "1                  Hardline Unionist   86.139977  12.769089  1.090934  71.0   \n",
       "2         Moderate Irish nationalist   76.872845  16.470596  6.656559  41.0   \n",
       "3                  Moderate Unionist   70.780267  25.171690  4.048043  30.0   \n",
       "4                      Non-sectarian   29.925636  64.432789  5.641575   5.0   \n",
       "5                         None/Other  100.000000        NaN       NaN   6.0   \n",
       "\n",
       "                           \n",
       "job_rgnl disagree neutral  \n",
       "0            13.0     NaN  \n",
       "1            10.0     1.0  \n",
       "2             9.0     4.0  \n",
       "3             8.0     2.0  \n",
       "4             9.0     1.0  \n",
       "5             NaN     NaN  "
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "evsfunction('job_rgnl',ni08,'partygrps')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
